package org.jeecg.modules.wlhy.hyplatform.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DictUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.wlhy.bill.entity.HyCustomerBill;
import org.jeecg.modules.wlhy.bill.service.IHyCustomerBillService;
import org.jeecg.modules.wlhy.hyplatform.dto.HyPlatformCostDTO;
import org.jeecg.modules.wlhy.hyplatform.entity.HyPlatformCost;
import org.jeecg.modules.wlhy.hyplatform.service.IHyPlatformCostService;
import org.jeecg.modules.wlhy.hyproject.service.IHyProjectService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Api(tags="hy_platform_cost")
@RestController
@RequestMapping("/hyplatform/hyPlatformCost")
@Slf4j
public class HyPlatformCostController extends JeecgController<HyPlatformCost, IHyPlatformCostService> {
	@Autowired
	private IHyPlatformCostService hyPlatformCostService;

	@Autowired
	private IHyShippingNoteService hyShippingNoteService;

	@Autowired
	private IHyCustomerBillService customerBillService;

	@Autowired
	private UserUtil userUtil;

	@Autowired
	private DictUtil dictUtil;


	@ApiOperation(value="hy_platform_cost-分页列表查询", notes="hy_platform_cost-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyPlatformCostDTO hyPlatformCostDto,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {

		HyPlatformCost hyPlatformCost = new HyPlatformCost();
		BeanUtil.copyProperties(hyPlatformCostDto, hyPlatformCost, true);
		QueryWrapper<HyPlatformCost> queryWrapper = QueryGenerator.initQueryWrapper(hyPlatformCost, req.getParameterMap());

		//根据运单号反向查询垫付记录
		if (StrUtil.isNotEmpty(hyPlatformCostDto.getShippingNoteNumber())) {
			List<HyShippingNote> hyShippingNoteList = this.hyShippingNoteService.list(
					new LambdaQueryWrapper<HyShippingNote>()
							.like(HyShippingNote::getShippingNoteNumber, hyPlatformCostDto.getShippingNoteNumber())
							.isNotNull(HyShippingNote::getPlatformCostId)
			);

			if (CollUtil.isEmpty(hyShippingNoteList)) {
				return Result.OK();
			}

			List<String> costIds = hyShippingNoteList.stream().map(s->s.getPlatformCostId()).collect(Collectors.toList());
			queryWrapper.in("id", costIds);
		}

		Page<HyPlatformCost> page = new Page<HyPlatformCost>(pageNo, pageSize);
		IPage<HyPlatformCost> pageList = hyPlatformCostService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @GetMapping(value = "/tj")
	 public Result<?> orderTj(@RequestParam(required = false) Map<String,String> params) {
		 List<?> list = dictUtil.parseArrayListDict(hyPlatformCostService.tj(params));
		 return Result.OK(list);
	 }

	 @AutoLog(value = "hy_platform_cost-还款")
	 @ApiOperation(value="hy_platform_cost-还款", notes="hy_platform_cost-还款")
	 @PutMapping(value = "/hk")
	 @RequiresPermissions("platform:hk")
	 @Transactional
	 public Result<?> hk(@RequestParam(name="id",required=true) String id,@RequestParam(name="billFile",required=false ,defaultValue = "") String billFile) {
		 if (!userUtil.isSys()) {
			 return Result.error("没有权限操作!");
		 }
		 HyPlatformCost entity = hyPlatformCostService.getByIdForUpdate(id);
		 if (entity == null) {
			 return Result.error("记录不存在!");
		 }
		 if (!"0".equals(entity.getSt())) {
			 return Result.error("已还款,不能重复操作!");
		 }

		 BigDecimal backMoney = entity.getBackMoney();
		 if(backMoney == null || backMoney.compareTo(BigDecimal.ZERO) == 0){
			 entity.setBackMoney(entity.getAmt());
		 }else {
			 return Result.error("分期未还完，不能执行此操作");
		 }

		 if (!customerBillService.checkPayComplate(id)) {
			 return Result.error("支付未完成,不能操作!");
		 }

		 entity.setSt("1");
		 entity.setBillFile(billFile);
		 hyPlatformCostService.updateById(entity);
		 return Result.OK("操作成功!");
	 }


//	/**
//	 *   添加
//	 *
//	 * @param hyPlatformCost
//	 * @return
//	 */
//	@AutoLog(value = "hy_platform_cost-添加")
//	@ApiOperation(value="hy_platform_cost-添加", notes="hy_platform_cost-添加")
//	@PostMapping(value = "/add")
//	public Result<?> add(@RequestBody HyPlatformCost hyPlatformCost) {
//		hyPlatformCostService.save(hyPlatformCost);
//		return Result.OK("添加成功！");
//	}

//	/**
//	 *  编辑
//	 *
//	 * @param hyPlatformCost
//	 * @return
//	 */
//	@AutoLog(value = "hy_platform_cost-编辑")
//	@ApiOperation(value="hy_platform_cost-编辑", notes="hy_platform_cost-编辑")
//	@PutMapping(value = "/edit")
//	public Result<?> edit(@RequestBody HyPlatformCost hyPlatformCost) {
//		hyPlatformCostService.updateById(hyPlatformCost);
//		return Result.OK("编辑成功!");
//	}

//	/**
//	 *   通过id删除
//	 *
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "hy_platform_cost-通过id删除")
//	@ApiOperation(value="hy_platform_cost-通过id删除", notes="hy_platform_cost-通过id删除")
//	@DeleteMapping(value = "/delete")
//	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
//		hyPlatformCostService.removeById(id);
//		return Result.OK("删除成功!");
//	}
//
//	/**
//	 *  批量删除
//	 *
//	 * @param ids
//	 * @return
//	 */
//	@AutoLog(value = "hy_platform_cost-批量删除")
//	@ApiOperation(value="hy_platform_cost-批量删除", notes="hy_platform_cost-批量删除")
//	@DeleteMapping(value = "/deleteBatch")
//	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
//		this.hyPlatformCostService.removeByIds(Arrays.asList(ids.split(",")));
//		return Result.OK("批量删除成功!");
//	}
//
//	/**
//	 * 通过id查询
//	 *
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "hy_platform_cost-通过id查询")
//	@ApiOperation(value="hy_platform_cost-通过id查询", notes="hy_platform_cost-通过id查询")
//	@GetMapping(value = "/queryById")
//	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
//		HyPlatformCost hyPlatformCost = hyPlatformCostService.getById(id);
//		if(hyPlatformCost==null) {
//			return Result.error("未找到对应数据");
//		}
//		return Result.OK(hyPlatformCost);
//	}
//
//    /**
//    * 导出excel
//    *
//    * @param request
//    * @param hyPlatformCost
//    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HyPlatformCost hyPlatformCost) {
        return super.exportXls(request, hyPlatformCost, HyPlatformCost.class, "待还款");
    }
//
//    /**
//      * 通过excel导入数据
//    *
//    * @param request
//    * @param response
//    * @return
//    */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//        return super.importExcel(request, response, HyPlatformCost.class);
//    }

}
